package com.zbvc.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zbvc.dao.AdminDao;
import com.zbvc.dao.impl.AdminDaoImpl;
import com.zbvc.entity.Admin;

/**
 * Servlet implementation class RegisterServlet
 */
@WebServlet(name = "reg", urlPatterns = { "/reg" })
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	
	private AdminDao adminDao = new AdminDaoImpl();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 防止中文乱码
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		
		String username = request.getParameter("username"); // 用户名
		String password = request.getParameter("password"); // 密码
		String name = request.getParameter("name");// 姓名
		String email = request.getParameter("email");
		String phone = request.getParameter("phone");
		Admin admin = new Admin();
		admin.setUsername(username);
		admin.setPassword(password);
		admin.setName(name);
		admin.setEmail(email);
		admin.setPhone(phone);
		admin.setStatus(1);
		int row = adminDao.reg(admin);
		if(row>0) {
		System.out.println("注册成功");
		response.sendRedirect("/book-manager/login.jsp");
		}else {
			System.out.println("注册失败");
		}
		
		
		
	}

}
